﻿using System.Diagnostics;

using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio.TextManager.Interop;

namespace Microsoft.WowAddonStudio.Lua.LanguageService.Model
{
    public class LuaSource : Source
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LuaSource"/> class.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <param name="textLines">The text lines.</param>
        /// <param name="colorizer">The colorizer.</param>
        public LuaSource(LuaLanguageService service, IVsTextLines textLines, Colorizer colorizer)
            : base(service, textLines, colorizer)
        {
        }

        /// <summary>
        /// Gets the comment format.
        /// </summary>
        /// <returns>An instance of the <see cref="CommentInfo"/> class.</returns>
        public override CommentInfo GetCommentFormat()
        {
            return Configuration.CommentInfo;
        }

        //public override CompletionSet CreateCompletionSet()
        //{
        //    return new LuaCompletionSet(LanguageService.GetImageList(), this);
        //}

        /// <summary>
        /// Creates the authoring sink.
        /// </summary>
        /// <param name="reason">The parse reason.</param>
        /// <param name="line">The line.</param>
        /// <param name="col">The column.</param>
        /// <returns></returns>
        public override AuthoringSink CreateAuthoringSink(ParseReason reason, int line, int col)
        {
            return new LuaAuthoringSink(reason, line, col, LanguageService.Preferences.MaxErrorMessages);
        }

#if AUTOCOMPLETE
        public override void OnCommand(IVsTextView textView, VSConstants.VSStd2KCmdID command, char ch)
        {
            base.OnCommand(textView, command, ch);

            if (command == VSConstants.VSStd2KCmdID.TYPECHAR)
            {
                Trace.WriteLine("TypeChar: " + ch);

                int line, col;
                ErrorHandler.ThrowOnFailure(textView.GetCaretPos(out line, out col));

                TokenInfo tokenInfo = this.GetTokenInfo(line, col);

                // Trigger a CompleteWord parse if an identifier is being typed and the completor is not active
                if (tokenInfo.Type == TokenType.Identifier && tokenInfo.EndIndex < tokenInfo.StartIndex + 1 && !this.IsCompletorActive)
                    this.Completion(textView, tokenInfo, ParseReason.DisplayMemberList);
            }
        }
#endif
    }
}